package com.xcm.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateUtils {

    public static String PATTEN_YMD = "yyyy-MM-dd";
    public static String PATTEN_YMD_HMS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 把long类型日期转化为格式化字符串日期
	 * @param longTimeStamp long类型时间戳
	 * @param pattern   格式
	 * @return  String(格式化后的日期)
	 */
	public static String longTimeStampToStringDate(Long longTimeStamp, String pattern) {
		if (longTimeStamp == null || StringUtils.isBlank(pattern)) {
			throw new NullPointerException("时间戳或格式类型不能为空");
		}
		Date date = new Date(longTimeStamp);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		String formatDate = simpleDateFormat.format(date);
		return formatDate;
	}

	/**
	 * 把字符串日期转换为LonG类型日期
	 * @param time 字符串日期
	 * @param pattern 格式
	 * @return Long日期
	 * @throws ParseException
	 */
	public static Long stringTimeToLongTimeStamp(String time,String pattern) throws ParseException {
		if(StringUtils.isBlank(time) || StringUtils.isBlank(pattern)) {
			throw new NullPointerException("时间或格式类型不能为空");
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		Long formateDate = simpleDateFormat.parse(time).getTime();
		return formateDate;
	}

	/**
	 * 获取当前时间字符串
	 * @return
	 */
	public static String getCurrentDateStr(String patten){
	    if(StringUtils.isBlank(patten)){
            patten = PATTEN_YMD_HMS;
        }
		SimpleDateFormat tempDate = new SimpleDateFormat(patten);
		return tempDate.format(new Date());
	}
}
